*-------------------------------------------------
* This code run all regression analysis to produce the following tables
* Before running this code, you should run DEP_META_cleean_data.R file to have your data cleaned for regression analysis
*-------------------------------------------------
set more off

* --- set up your working directory here 
global data_dir ""
* specify output folder here
global project_dir "" 

* --- change directory 
cd "${data_dir}/processed/"

*------------------------------------------------------------------------------
* --- data cleaning function
*==============================================================================

cap: program drop process_var
program process_var
rename psuscid_x psuscid
rename region_x region 
rename grade_x grade 
gen cohort_size = N_peer

replace grade = . if grade == 6 | grade == 13
tab grade,gen(grade)

gen dp_0 = prank_depress 

gen out_sample = 0 if ~missing(gswgt2) & ~missing(gswgt1) 
replace out_sample = 1 if out_sample == 0 & sqid == ""
replace out_sample = 2 if out_sample == 0 & missing(grade)
replace out_sample = 2 if out_sample == 0 & missing(peer_depress_median)
replace out_sample = 3 if out_sample == 0 & missing(fcesd_2)
replace out_sample = 4 if out_sample == 0 & missing(depress)
replace out_sample = 5 if out_sample == 0 & missing(parent_attachment)

local cv0 female white black hispanic other grade immig_1st immig_2nd immig_3rd  family_two family_one family_other pa_educ assistance sibsize pvt 
egen n_miss = rowmiss(`cv0') if out_sample == 0
replace out_sample = 8 if out_sample == 0 & n_miss != 0

gen subgr = (out_sample == 0) if ~missing(out_sample)

* adjust some variables 
local peeriv peer_depress_q95 peer_depress_q05 peer_depress_q90 peer_depress_q10 peer_depress_q15 peer_depress_q20 peer_depress_q25
local peercv female_mean white_mean black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean immig_3rd_mean family_two_mean family_one_mean family_other_mean  dp_0

foreach x of local peercv {
	replace `x' = `x'* 100
}

foreach x of local peeriv {
	replace `x' = `x'* 100
}

* label some measures
label var parent_attachment "Parent-child attachment"
label var peer_depress_q95 "Peer Depression Top 5"
label var peer_depress_q90 "Peer Depression Top 10"
label var peer_depress_q05 "Peer Depression Bottom 5"
label var peer_depress_q10 "Peer Depression Bottom 10"
label var peer_depress_q15 "Peer Depression Bottom 15"
label var peer_depress_q20 "Peer Depression Bottom 20"
label var peer_depress_q25 "Peer Depression Bottom 25"
label var dp_0 "Prior depression (% rank)"

end 

*------------------------------------------------------------------------------
* analysis : Table 1
*==============================================================================
use reg_main_grade_all_include_self, clear 
* process data 
process_var 

* sample size across sample selection criteria
tab out_sample

* ------
* Table 1 : Descriptive Statistcis for Study Variables
* ------
local cv female white black hispanic other immig_1st immig_2nd immig_3rd family_two family_step family_one family_other pa_educ assistance sibsize pvt dp_0 grade1-grade6 
local peercv female_mean white_mean black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean immig_3rd_mean family_two_mean family_one_mean family_other_mean pa_educ_mean cohort_size
local peeriv peer_depress_median peer_depress_q95 peer_depress_q05

* summary statistics for analytic sample
sum fcesd_2 `peeriv' parent_attachment `cv' `peercv' [aw=gswgt2] if subgr == 1
* summary statistics for full sample
sum fcesd_2 `peeriv' parent_attachment `cv' `peercv' [aw=gswgt2]

* for tables : Table 1
tabstat fcesd_2 `peeriv' parent_attachment `cv' `peercv' [aw=gswgt2], column(statistics) stat(count mean sd min p25 p50 p75 max), if subgr == 1

* ------
* Table 2 : Effects of Peer Depression on Adolescent Depressive Symptoms
* ------
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size

* main effect models 
estimates clear
local dv fcesd_2
reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv'  [pw=gswgt2] if subgr == 1 
estimates store ols1
reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 [pw=gswgt2]  if subgr == 1 
estimates store ols2
reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2] if subgr == 1 
estimates store ols3
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store fe1
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store fe2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store fe3

esttab * using "${project_dir}/table2.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ order(peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment dp_0) replace mtitle

* ------
* Table 3. Effects of Peer Depression on Adolescent Depressive Symptoms, by Type of Peer Group
* ------
estimates clear 
foreach peer in grade local_position club friend_all {
	use reg_main_`peer'_all_include_self, clear 	
	process_var

	local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
	local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
	local dv fcesd_2

	areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
	estimates store m`peer'
}

esttab * using "${project_dir}/table3_fe.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ order(peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment dp_0) replace mtitle

* ------
* Table 3. Effects of Peer Depression on Adolescent Depressive Symptoms, by Type of Peer Group
* In response to reviewers, Table R1. using OLS models
* ------
estimates clear 
foreach peer in grade local_position club friend_all {
	use reg_main_`peer'_all_include_self, clear 	
	process_var

	local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
	local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
	local dv fcesd_2

	reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2] if subgr == 1 
	estimates store m`peer'
}

esttab * using "${project_dir}/table3_ols.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ order(peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment dp_0) replace mtitle

* ------
* Table 4 : interaction effects
* ------
use reg_main_grade_all_include_self, clear 
* process data 
process_var 

estimates clear
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment < 5
estimates store fe_low
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0[pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment == 5
estimates store fe_high
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' `peercv' dp_0 c.peer_depress_median#c.parent_attachment c.peer_depress_q95#c.parent_attachment c.peer_depress_q05#c.parent_attachment [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store fe_full
esttab * using "${project_dir}/table4.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ order(peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment c.peer_depress_median#c.parent_attachment c.peer_depress_q95#c.parent_attachment c.peer_depress_q05#c.parent_attachment) replace mtitle

* ------
* Table 4 : interaction effects
* In response to reviewers, Table R2. using alternative definition | OLS models
*. tab parent_attachment if subgr == 1
*
*Parent-chil |
*          d |
* attachment |      Freq.     Percent        Cum.
*------------+-----------------------------------
*          1 |         28        0.34        0.34
*        1.5 |         13        0.16        0.49
*          2 |         76        0.92        1.41
*        2.5 |         35        0.42        1.83
*          3 |        204        2.46        4.29
*        3.5 |        201        2.42        6.72
*          4 |        731        8.82       15.54
*        4.5 |        647        7.80       23.34
*          5 |      6,355       76.66      100.00
*------------+-----------------------------------
*      Total |      8,290      100.00
* ------

estimates clear
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2
reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2] if subgr == 1 & parent_attachment < 5
estimates store ols_low
reg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2] if subgr == 1 & parent_attachment == 5
estimates store ols_high
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment < 4.5
estimates store fe_low
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0[pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment >= 4.5
estimates store fe_high
esttab * using "${project_dir}/table4_alternative.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ order(peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment) replace mtitle

* ------
* for marginal effects plots : Figure 3
* ------
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2

qui areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment < 5
margins, at(peer_depress_q95 =(1 10))

qui areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment == 5
margins, at(peer_depress_q95 =(1 10))

* for marginal plots 
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2

qui areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment < 4.5
margins, at(peer_depress_q95 =(1 10))

qui areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05  `cv' `peercv' dp_0 [pw=gswgt2], absorb(scid), if subgr == 1 & parent_attachment >= 4.5
margins, at(peer_depress_q95 =(1 10))

* ------
* Table A3. Balancing Tests
* ------
use reg_main_grade_all_include_self, clear 
* process data 
process_var 

* survey set
svyset psuscid [pweight=gswgt1], strata(region)

local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size

estimates clear
svy, subpop(subgr): reg peer_depress_median parent_attachment `cv' dp_0 `peercv', absorb(scid), if subgr == 1
estimates store mmedian 
svy, subpop(subgr): reg peer_depress_q95 parent_attachment `cv' dp_0 `peercv', absorb(scid), if subgr == 1
estimates store m95 
svy, subpop(subgr): reg  peer_depress_q05 parent_attachment `cv' dp_0 `peercv', absorb(scid), if subgr == 1
estimates store m05

esttab * using "${project_dir}/table_a3.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/ replace mtitle

* ----
* Table 5. Additional sensitivity checks 
* ----
use reg_main_grade_all_include_self, clear 
process_var 

local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size

gen binary_fcesd_2 = 0 if ~missing(fcesd_2)
replace binary_fcesd_2 = 1 if fcesd_2 >= 22 & female == 0
replace binary_fcesd_2 = 1 if fcesd_2 >= 24 & female == 1

estimates clear
* original specification 
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m1 
* linear probability model
local dv binary_fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m2
* use mean peer depression instead of median
local dv fcesd_2
areg `dv' i.grade peer_depress_mean peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m3
* use q90-q10
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q90 peer_depress_q10 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m4
* use q95-q10
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q10 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m4_10
* use q95-q15
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q15 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m4_15
* use q95-q20
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q20 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m4_20
* use q95-q25
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q25 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m4_25
* exclude self
use reg_main_grade_all_exclude_self, clear 
process_var 
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m5
* use grade as a reference
use reg_main_grade_grade_include_self, clear 
process_var 
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m6
* use school as a reference
use reg_main_grade_school_include_self, clear 
process_var 
local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2
areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 
estimates store m7
* work on small/large schools
use reg_main_grade_all_include_self, clear 
process_var 

* define small/large schools
gen large_school = 0 if ~missing(cohort_size)
replace large_school = 1 if cohort_size > 545

gen small_school = 0 if ~missing(cohort_size)
replace small_school = 1 if cohort_size < 29 

local cv female black hispanic other immig_1st immig_2nd family_step family_one family_other pa_educ assistance sibsize pvt
local peercv female_mean  black_mean hispanic_mean other_mean immig_1st_mean immig_2nd_mean  family_one_mean family_other_mean pa_educ_mean cohort_size
local dv fcesd_2

areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 & small_school == 0
estimates store m8

areg `dv' i.grade peer_depress_median peer_depress_q95 peer_depress_q05 parent_attachment `cv' dp_0 `peercv'  [pw=gswgt2], absorb(scid), if subgr == 1 & large_school == 0
estimates store m9

esttab * using "${project_dir}/table5_sensitivity.csv", csv star(+ 0.1 * 0.05 ** 0.01 *** 0.001) se nogap label r2 ar2 scalar(N_sub) b(%9.3f) /*
*/  replace mtitle
